package com.wust.loveangel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.wust.R;
import com.wust.data.KnowledgeMap;
import com.wust.data.MapDao;
import com.wust.footprint.FirstActivity;
import com.wust.menu.FirstGroupTab;
import com.wust.menu.ThirdGroupTab;

public class loveAngelActivity extends ListActivity {
	private ListView listview=null;
	private ArrayList<HashMap<String, Object>> listItems;
	private TextView loveangel_title=null;
	private MapDao mapDao;
	private List<KnowledgeMap>maplist;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.loveangel);
		loveangel_title=(TextView)findViewById(R.id.loveangel_title);
		loveangel_title.setText("爱心小天使");
		mapDao=new MapDao(this);
       
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		listview=getListView();
		listview.setAdapter(buildAdapter());

		super.onResume();
	}

	 private  SimpleAdapter buildAdapter(){
			listItems = new ArrayList<HashMap<String, Object>>(); 
			maplist=mapDao.queryMapByparentId("0");
			for (Iterator iterator = maplist.iterator(); iterator.hasNext();) {
				KnowledgeMap kmap = (KnowledgeMap) iterator.next();
				HashMap<String, Object> map = new HashMap<String, Object>();
				map.put("loveangel_item_title", kmap.getCat_name());
				listItems.add(map);
			}
	    
	           
	        //生成适配器的Item和动态数组对应的元素       
	        SimpleAdapter listItemAdapter = new SimpleAdapter(this,listItems,//数据源        
	            R.layout.loveangel_first_item,//ListItem的XML布局实现       
	            //动态数组与ImageItem对应的子项               
	            new String[] {"loveangel_item_title"},        
	            //ImageItem的XML文件里面的一个ImageView,两个TextView ID       
	            new int[] {R.id.loveangel_item_title}       
	        ); 
	        return listItemAdapter;
		}
		private void buildList(){
			listview.setAdapter(buildAdapter());
		}
		private void jumptoActivity(Class activity,String activityname ,String key[],String value[]){
			Intent intent = new Intent(loveAngelActivity.this, activity);  
			if(key!=null&&value!=null&&key.length==value.length)
			
				for(int i=0;i<key.length;++i)
					intent.putExtra(key[i], value[i]);
//				intent.putExtra("user_id",user_id);
			
		
	        // 把Activity转换成一个Window，然后转换成View  
	        Window w = ThirdGroupTab.group.getLocalActivityManager()  
	                .startActivity(activityname,intent);  
	        View view = w.getDecorView();  
	        //设置要跳转的Activity显示为本ActivityGroup的内容  
	        ThirdGroupTab.group.setContentView(view); 
		}
		@Override
		protected void onListItemClick(ListView l, View v, int position, long ids) {
			// TODO Auto-generated method stub
			
			super.onListItemClick(l, v, position, ids);
			//要跳转的Activity  
			Intent intent=new Intent();
			intent.setClass(this, loveAngelActivity2.class);
			String msg=null;
			
			msg=String.valueOf(maplist.get(position).getId());
			jumptoActivity(loveAngelActivity2.class,"loveAngelActivity2",new String[]{"msg"},new String[]{msg});	
		}
}
